﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SqlCeViewer.Base;
using System.Reflection;
using CustomControl.DynaProgressBar;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars;
using SqlCeViewer.CommonMethod;
using SqlCeViewer.Utility;
using Microsoft.Practices.EnterpriseLibrary.Data.SqlCe;
using XLCS.Common;
using CustomControl.NewXLAGControl;
using System.Data.Common;
using DevExpress.XtraEditors;
using SqlCeViewer.UI;

namespace SqlCeViewer.MainForm
{
    public partial class Main 
{
        public enum CurCommandType
        {
            Query,
            Insert,
            Update,
            Delete,
            SystemAction,
            CreateTable,
            Unknown,
        }

        private CurCommandType curCmdType;

        public CurCommandType _curCmdType
        {
            get { return curCmdType; }
            set { curCmdType = value; }
        }


        private void Do_CreateTableCmd(DbCommand cmd)
        {
            try
            {
                int resultCount = GlobalInfo.MainEngineer.DbHandler.ExecuteNonQuery(cmd);
                if (resultCount == -1)
                {
                    MessageBox.Show(String.Format("Table {0} has created ", 1));
                    RefreshCurrentDbStatus();
                }
                else
                {
                    MessageBox.Show(String.Format("Table has failure"));
                }
            }
            catch (Exception ee)
            {
                ProcessException.DisplayErrors(ee);
            }
        }

        private void Do_InsertCmd(DbCommand cmd)
        {
            try
            {
                int resultCount = GlobalInfo.MainEngineer.DbHandler.ExecuteNonQuery(cmd);

                MessageBox.Show(String.Format("{0} has changed ", resultCount));
            }
            catch (Exception ee)
            {
                ProcessException.DisplayErrors(ee);
            }
        }

        private void Do_SysCmd(DbCommand cmd)
        {
            try
            {
                object result = GlobalInfo.MainEngineer.DbHandler.ExecuteScalar(cmd);
                MessageBox.Show(String.Format("{0} has changed ", result));
            }
            catch (Exception ee)
            {
                ProcessException.DisplayErrors(ee);
            }
            RefreshCurrentDbStatus();
        }

        private void RefreshCurrentDbStatus()
        {

            if (GlobalInfo.MainEngineer.GetTableList() != null)
            {
                Main_Load(null, null);
            }
            else
            {
                throw new Exception("System Query Error");
            }
        }

        private void Do_DeleteCmd(DbCommand cmd)
        {
            try
            {
                object result = GlobalInfo.MainEngineer.DbHandler.ExecuteScalar(cmd);
                MessageBox.Show(String.Format("{0} has changed ", result));

            }
            catch (Exception ee)
            {
                ProcessException.DisplayErrors(ee);
            }

        }

        private void Do_UpdateCmd(DbCommand cmd)
        {
            try
            {
                object result = GlobalInfo.MainEngineer.DbHandler.ExecuteScalar(cmd);
                MessageBox.Show(String.Format("{0} has changed ", result));

            }
            catch (Exception ee)
            {
                ProcessException.DisplayErrors(ee);
            }

        }

        private void Do_SelectCmd(DbCommand cmd)
        {
            try
            {
                StartProcessingBar();
                DataSet ds = GlobalInfo.MainEngineer.DbHandler.ExecuteDataSet(cmd);

                FillUI(ds);

            }
            catch (Exception ee)
            {
                ProcessException.DisplayErrors(ee);
            }
            finally
            {

                StopProcessingBar();
            }
        }

        private void FillUI(DataSet ds)
        {
            TempDataForm t = new TempDataForm();
            t.gridView1.Columns.Clear();
            
            t.gridControl1.DataSource = ds.Tables[0];

            t.MdiParent = this;
            t.WindowState = FormWindowState.Maximized;
            t.Show();
        }
        /// <summary>
        /// parse sql command ,and type
        /// </summary>
        /// <param name="cmdText"></param>
        /// <returns></returns>
        private string ParseCmd(string cmdText)
        {

            string result = string.Empty;
            if (string.IsNullOrEmpty(cmdText))
            {
                ProcessException.DisplayErrors(new Exception("Empty Command"));
            }
            try
            {
                cmdText = cmdText.TrimStart();

                cmdText = cmdText.TrimEnd();

                cmdText = cmdText.ToLower();

                if (cmdText.StartsWith("select"))
                {
                    _curCmdType = CurCommandType.Query;
                }
                else if (cmdText.StartsWith("insert"))
                {
                    _curCmdType = CurCommandType.Insert;
                }
                else if (cmdText.StartsWith("update"))
                {
                    _curCmdType = CurCommandType.Update;
                }
                else if (cmdText.StartsWith("delete"))
                {
                    _curCmdType = CurCommandType.Delete;
                }
                else if (cmdText.StartsWith("alter") || (cmdText.StartsWith("drop")))
                {
                    _curCmdType = CurCommandType.SystemAction;
                }
                else if (cmdText.StartsWith("create"))
                {
                    _curCmdType = CurCommandType.CreateTable;
                }
                else
                {
                    _curCmdType = CurCommandType.Unknown;
                }

                result = cmdText;
            }
            catch (Exception ee)
            {
                ProcessException.DisplayErrors(ee);
            }

            return result;
        }

    }
}
